//
//  GridMeViewController.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/3/23.
//

import UIKit
import SnapKit

class GridMeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = Theme.Color.Background.default
        navigationItem.title = "我的"
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
    
    lazy var tableView: UITableView = {
        let tv = UITableView.init(frame: CGRect.zero, style: .grouped)
        tv.backgroundColor = UIColor.white
        tv.separatorStyle = .none
        tv.delegate = self
        tv.dataSource = self
//        tv.tableHeaderView = headerView
        tv.register(GridMeHeaderCell.self, forCellReuseIdentifier: "GridMeHeaderCell")
        tv.register(GridMeUpdateCell.self, forCellReuseIdentifier: "GridMeUpdateCell")
        tv.register(GridMeMessageCell.self, forCellReuseIdentifier: "GridMeMessageCell")
        
        view.addSubview(tv)
        return tv
    }()
    
    lazy var loginOutView: GridMeLoginOutView = {
        let view = GridMeLoginOutView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 200))
        return view
    }()

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension GridMeViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "GridMeHeaderCell", for: indexPath) as! GridMeHeaderCell
            return cell
        }else if indexPath.row > 0 && indexPath.row < 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "GridMeMessageCell", for: indexPath) as! GridMeMessageCell
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "GridMeUpdateCell", for: indexPath) as! GridMeUpdateCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 60
        }
        else if indexPath.row > 0 && indexPath.row < 3 {
            return 44
        }
        return 50
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 200
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return loginOutView
    }
}

extension GridMeViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

    }
}
